﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using StandardLibrary;
using StandardFramework;
using GenericUndoRedo;


namespace PolarExpress
{
    abstract class ComponentMemento : IMemento<BaseComponentPool>
    {
        #region IMemento<ComponentPool> Members

        public abstract IMemento<BaseComponentPool> Restore(BaseComponentPool target);

        #endregion
    }

    class InsertComponentMemento : ComponentMemento
    {
        private int index;
        public InsertComponentMemento(int index)
        {
            this.index = index;
        }

        public override IMemento<BaseComponentPool> Restore(BaseComponentPool target)
        {
            BaseComponent removed = target[index];
            IMemento<BaseComponentPool> inverse = new RemoveComponentMemento(index, removed);
            target.RemoveAt(index);
            return inverse;
        }
    }

    class RemoveComponentMemento : ComponentMemento
    {
        BaseComponent removed;
        int index;
        public RemoveComponentMemento(int index, BaseComponent removed)
        {
            this.index = index;
            this.removed = removed;
        }

        public override IMemento<BaseComponentPool> Restore(BaseComponentPool target)
        {
            IMemento<BaseComponentPool> inverse = new InsertComponentMemento(index);
            target.Insert(index, removed);
            return inverse;
        }
    }

    class AddComponentMemento : ComponentMemento
    {
        public AddComponentMemento()
        {
        }

        public override IMemento<BaseComponentPool> Restore(BaseComponentPool target)
        {
            int index = target.Count - 1;
            IMemento<BaseComponentPool> inverse = new RemoveComponentMemento(index, target[index]);
            target.RemoveAt(target.Count - 1);
            return inverse;
        }
    }

    class MoveComponentMemento : ComponentMemento
    {
        int _x;
        int _y;

        BaseComponent _target;

        public MoveComponentMemento(int x, int y, BaseComponent target)
        {
            _x = x;
            _y = y;

            _target = target;
        }

        public override IMemento<BaseComponentPool> Restore(BaseComponentPool target)
        {
            IMemento<BaseComponentPool> inverse = new MoveComponentMemento(-_x,-_y,_target);

            BoundComponentData boundComp = _target.GetComponent(typeof(BoundComponentData)) as BoundComponentData;

            boundComp.X = boundComp.Bound.X - _x;
            boundComp.Y = boundComp.Bound.Y - _y;

            return inverse;
        }
    }
}
